package sunwin.yoms.service.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.dao.BlogrollDao;
import sunwin.yoms.domain.system.BlogrollDomain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watson on 2015/7/30.
 */
@Service
public class BlogrollService {
    @Autowired
    private BlogrollDao blogrollDao;

    /**
     * @param id 上移
     * @throws YomsException
     */
    public void Up(int id) throws YomsException {
        //获取id为1的百度
        BlogrollDomain domain1 = this.getBrogrollById(id);
        if (domain1.getBlogrollIndex() == 1) {
            throw new YomsException("该数据已是第一条");
        }

        BlogrollDomain condition = new BlogrollDomain();
        condition.setBlogrollIndex(domain1.getBlogrollIndex() - 1);
        //新浪
        List<BlogrollDomain> bases = this.blogrollDao.getDomainByCondition(condition);

        BlogrollDomain domain2 = bases.get(0);

        domain1.setBlogrollIndex(domain1.getBlogrollIndex() - 1);
        domain2.setBlogrollIndex(domain2.getBlogrollIndex() + 1);

        blogrollDao.updateDomain(domain1);
        blogrollDao.updateDomain(domain2);
    }

    /**
     * @param id 下移
     * @throws YomsException
     */

    public void Down(int id) throws YomsException {
        BlogrollDomain blogrollDomain1 = this.getBrogrollById(id);

        if (blogrollDomain1.getBlogrollIndex() == blogrollDao.getDomainCount()) {
            throw new YomsException("该数据已是最后一条");
        }
        BlogrollDomain condition2 = new BlogrollDomain();
        //赋值
        condition2.setBlogrollIndex(blogrollDomain1.getBlogrollIndex() + 1);

        List<BlogrollDomain> bases = this.blogrollDao.getDomainByCondition(condition2);


        BlogrollDomain domain2 = bases.get(0);

        blogrollDomain1.setBlogrollIndex(blogrollDomain1.getBlogrollIndex() + 1);

        domain2.setBlogrollIndex(domain2.getBlogrollIndex() - 1);

        blogrollDao.updateDomain(blogrollDomain1);
        blogrollDao.updateDomain(domain2);


    }

    /**
     * @param blogrollDomain 查询
     * @return
     * @throws YomsException
     */
    public List<BlogrollDomain> findBrogroll(BlogrollDomain blogrollDomain) throws YomsException {
        List<BlogrollDomain> blogrollDomains = new ArrayList<BlogrollDomain>();
        blogrollDomains = blogrollDao.getDomainCollection(blogrollDomain);
        if (blogrollDomains.size() == 0) {
            throw new YomsException("没有找到数据", 1);
        }
        return blogrollDomains;
    }

    /**
     * 根据page获取友情链接
     */
    public List<BlogrollDomain> getBlogrollByPage(int pageIndex, int pageSize) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        yomsPage.setPageSize(pageSize);
        Page page = PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        List<BlogrollDomain> domains = blogrollDao.getDomainCollection(null);

        long total = page.getTotal();
        long pageTotal = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        if (pageIndex > pageTotal) {
            domains.clear();
        }

        return domains;
    }

    /**
     * id查询
     *
     * @param blogrollId
     * @return
     * @throws YomsException
     */
    public BlogrollDomain getBrogrollById(int blogrollId) throws YomsException {
        BlogrollDomain domain = blogrollDao.getDomainById(blogrollId);
        if (domain == null) {
            throw new YomsException("该ID不存在", 1);
        }
        return domain;
    }

    /**
     * 总数量
     *
     * @return
     */
    public int getBrogrollCount() {
        return blogrollDao.getDomainCount();
    }

    /**
     * 增加
     *
     * @param blogrollDomain
     * @throws YomsException
     */
    public void createBrogroll(BlogrollDomain blogrollDomain) throws YomsException {
        BlogrollDomain blogrolls = new BlogrollDomain();
        blogrolls.setBlogrollName(blogrollDomain.getBlogrollName());
        List<BlogrollDomain> blogrollDomains = blogrollDao.getDomainByCondition(blogrolls);
        if (blogrollDomain.getBlogrollUrl().contains("http://")) {
            blogrollDomain.setBlogrollUrl(blogrollDomain.getBlogrollUrl());
        } else {
            blogrollDomain.setBlogrollUrl("http://" + blogrollDomain.getBlogrollUrl());
        }

        if (blogrollDomains.size() > 0) {
            throw new YomsException("该链接已存在", 1);

        } else {
            int i = blogrollDao.getDomainCount();
            blogrollDomain.setBlogrollIndex(i + 1);
            blogrollDao.addDomain(blogrollDomain);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @throws YomsException
     */
    public void delBrogroll(int id) throws YomsException {
        BlogrollDomain blogrollDomain = new BlogrollDomain();
        blogrollDomain.setBlogrollId(id);
        BlogrollDomain domain = blogrollDao.getDomainById(id);
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (blogrollDao.deleteDomain(blogrollDomain) != 1) {
                throw new YomsException("删除链接失败");
            }

        }
    }

    /**
     * 更新
     *
     * @param blogrollDomain
     * @throws YomsException
     */
    public void updateBlogroll(BlogrollDomain blogrollDomain) throws YomsException {
        BlogrollDomain domain = blogrollDao.getDomainById(blogrollDomain.getBlogrollId());
        if (blogrollDomain.getBlogrollUrl().contains("http://")) {
            blogrollDomain.setBlogrollUrl(blogrollDomain.getBlogrollUrl());
        } else {
            blogrollDomain.setBlogrollUrl("http://" + blogrollDomain.getBlogrollUrl());
        }
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (blogrollDao.updateDomain(blogrollDomain) != 1) {
                throw new YomsException("更新友情链接失败");
            }
        }

    }
}
